import { Component, OnInit, ViewChild, Input } from '@angular/core';
import { InitService } from '../../public/serve/init/init.service';
import { appSynonymUrl } from '../synonym.url';
import { synonym } from '../synonym.data';
import { SynonymManagementTableComponent } from './synonym-management-table/synonym-management-table.component';
import { SynonymManagementLeftComponent } from './synonym-management-left/synonym-management-left.component';
import { RemoveTableService } from '../../public/serve/remove-table/remove-table.service';
import { Service } from '../../server/server';
import { HintsService } from '../../public/serve/hints/hints.service';
import { CudTableService } from '../../public/serve/cud-table/cud-table.service';
import {MessageService} from 'primeng/api';
declare var $:any;
@Component({
  selector: 'app-synonym-management',
  templateUrl: './synonym-management.component.html',
  styleUrls: ['./synonym-management.component.css']
})
export class SynonymManagementComponent implements OnInit {

  @ViewChild("table") table : SynonymManagementTableComponent;
  @ViewChild("leftTableData") left : SynonymManagementLeftComponent;

  @Input() status: any  = '';
  @Input() searchData: any;

  tableNumber : number = 0;

  right: any = {
    "systemSynonym": {}
  };

  constructor(
    public initService: InitService,
    public removeTableService: RemoveTableService,
    public request: CudTableService,
    public server: Service,
    private hintsService: HintsService,
    private messageService:MessageService
  ) { 
    this.right = $.extend(this.right, server.getRight());
  }

  isActive = this.server.isActive;
  ngOnInit() {
    this.server.eventEmit.subscribe((value: any)=>{
      this.isActive = value;
    });
  }

  /**
   * 启用
   */
  _enable(){
    this._status(0);
  }

   /**
    * 禁用
    */
   _disable(){
    this._status(-1);
   }

 /**
   * 分页
   * @param pagination 
   */ 
  _pagination(pagination: Array<any>) {
    synonym.pagination.pageNumber = pagination[0].pageNumber;
    synonym.pagination.pageSize   = pagination[0].pageSize;
    synonym.pagination.naturalNum = (synonym.pagination.pageNumber - 1) * synonym.pagination.pageSize;
    this.table.getTabList();
  }

  //搜索
  search() {
    synonym.retrieval.queryCriteria = this.searchData;
    synonym.retrieval.status = this.status;
    this.table.getTabList();
  }
  //响应搜索
  changesearch() {
      this.search();
  }
  
  /**
   * 批量启用或禁用
   * @param val
   */
  private _status(val:any){

    this.removeTableService
    .confirmEnableDisable(synonym.retrieval.token,'ids',this.server.baseUrl + appSynonymUrl.updateSynonymStatus,'status',val)
    .then(res => {
      if(res.result_code == "0000"){
        this.table.getTabList();
      }
      this.hintsService.messages(res.message,res.result_code);
      return;
    })
    .catch(err =>{
      this.hintsService.messages('error','error');
      return;
    });
  }

    // 表格
    public leftTableData: any;
    ParentLeftData(leftTabledata: any) {
        this.left.leftTableData = leftTabledata;
        this.left.updateData();
    }

}
